package exhibition.app.nexd.cn.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnTextChanged;
import exhibition.app.nexd.cn.R;
import exhibition.app.nexd.cn.presenter.LoginPresenter;
import exhibition.app.nexd.cn.ui.ivew.ILoginActivityView;

public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginActivityView {

    @Bind(R.id.login_btn_login)
    Button loginBtn;

    @Bind(R.id.login_edit_phone)
    EditText phoneEdit;

    @Bind(R.id.login_edit_auth)
    EditText authEdit;

    @Bind(R.id.login_tv_auth)
    TextView authText;

    @Bind(R.id.login_edit_clear)
    ImageView ivClearPhone;

    @Bind(R.id.sendsms_iv_success)
    ImageView ivTrue;

    @Bind(R.id.login_auth_line)
    View viewLine;

    @Override
    public void initView() {
        getSupportActionBar().hide();
        authText.setEnabled(true);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initPresenter() {
        presenter = new LoginPresenter(this, this);
        presenter.init();
    }

    @OnClick({R.id.login_btn_login, R.id.login_tv_auth, R.id.login_edit_clear})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn_login:
                presenter.destoryTimer();
                // 开始登录
                presenter.userLogin(phoneEdit.getText().toString(), authEdit.getText().toString());
                break;
            case R.id.login_tv_auth:
                //获取手机验证码
                presenter.checkPhone(phoneEdit.getText().toString().trim());
                break;
            case R.id.login_edit_clear:
                phoneEdit.setText("");
                presenter.destoryTimer();
                break;
        }
    }

//    @OnFocusChange(R.id.login_edit_phone)
//    public void onFocusChange(View v, boolean hasFocus) {
//        if (!hasFocus) {
//            presenter.checkPhone(phoneEdit.getText().toString().trim());
//        }
//    }

    @OnTextChanged(value = R.id.login_edit_phone, callback = OnTextChanged.Callback.TEXT_CHANGED)
    public void onTextChange(CharSequence text) {
        if (TextUtils.isEmpty(text.toString())) {
            ivClearPhone.setVisibility(View.GONE);
        } else {
            ivClearPhone.setVisibility(View.VISIBLE);
        }
    }


    @Override
    public void showToast(String msg) {
        Toast.makeText(LoginActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCheckPhoneSuccess() {
        ivClearPhone.setVisibility(View.GONE);
        loginBtn.setBackgroundColor(getResources().getColor(R.color.nexd_main));
        loginBtn.setEnabled(true);
    }


    @Override
    public void onGetSMSCodeSuccess(String smsCode) {
        authEdit.setText(smsCode);
        ivTrue.setVisibility(View.VISIBLE);
        authText.setVisibility(View.GONE);
        viewLine.setVisibility(View.GONE);
    }

    @Override
    public void onGetSMSCodeFailed() {
        authText.setText("获取验证码");
        authText.setEnabled(true);
    }

    @Override
    public void onTimerChanged(boolean enable, String time) {
        authText.setEnabled(enable);
        authText.setText(time);
    }

    @Override
    public void onUserLoginSuccess(boolean isNewUser) {
        Intent intent;
        if (isNewUser) {
            intent = new Intent(LoginActivity.this, UserInfoActivity.class);
        } else {
            intent = new Intent(LoginActivity.this, MainActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.destoryTimer();
    }
}
